package service.impl;

import common.Constants.MapKey;
import common.Constants.ServiceConstant;
import common.Result;
import common.Util.MD5Util;
import dao.UserDao;
import dao.UserInfoDao;
import dataBean.UserDo;
import dataBean.UserInfoDo;
import org.springframework.beans.factory.annotation.Autowired;
import service.TokenService;
import service.UserService;

/**
 * Created by lxl on 2015/10/30.
 */
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao ;

    @Autowired
    private UserInfoDao userInfoDao ;

    @Override
    public Result login(UserDo userDo) {

        Result result = new Result() ;

        String password ;

        try {

            password  = userDao.selectUserByUsername(userDo.getUsername()).getPassword() ;

        }catch (Exception e ){

            System.out.println(e.toString());

            result.setSucces(false);
            result.setError(ServiceConstant.USER_EXIST_ERROR);

            return result ;

        }



        if( password.equals(MD5Util.toMD5(userDo.getPassword()))  ){

            result.setSucces(true);
            result.addParam(MapKey.TOKEN,MD5Util.toMD5(userDo.getUsername()));

        }else {

            result.setSucces(false);
            result.setError(ServiceConstant.LOGIN_ERROR);

        }

        return result;
    }

    @Override
    public Result addUser(UserInfoDo userInfoDo) {

        Result result = new Result() ;

        if( userInfoDao.selectUserInfoByName(userInfoDo.getName()) != null ){

            result.setError(ServiceConstant.USER_EXIST_ERROR);
            result.setSucces(false);

        } else {

            String password = MD5Util.toMD5("123456") ;

            UserDo userDo = new UserDo() ;

            userDo.setUsername(userInfoDo.getUsername());
            userDo.setPassword(password);

            try {

                //if failed return the origin
                userDao.insertUser(userDo);
                userInfoDao.insertUserInfo(userInfoDo);

            }catch (Exception e){

                System.out.println(e.toString());

                result.setError(ServiceConstant.USER_INSERT_ERROR);
                result.setSucces(false);

                return result ;
            }

            result.setSucces(true);

        }

        return result;
    }

    @Override
    public Result modifyPassword(UserDo userDo, String oldPassword) {

        Result result = new Result() ;

        String password ;

        try {

            password  = userDao.selectUserByUsername(userDo.getUsername()).getPassword() ;

        }catch (Exception e ){

            System.out.println(e.toString());

            result.setSucces(false);
            result.setError(ServiceConstant.USER_EXIST_ERROR);

            return result ;

        }


        if( !password.equals(MD5Util.toMD5(oldPassword)) ){

            result.setError(ServiceConstant.PASSWORD_ERROR);
            result.setSucces(false);

        }else {

            userDo.setPassword(MD5Util.toMD5(userDo.getPassword()));

            try {

                userDao.updateUser(userDo) ;

            }catch (Exception e){

                System.out.println(e.toString());

                result.setError(ServiceConstant.USER_INSERT_ERROR);
                result.setSucces(false);

                return result ;

            }

            result.setSucces(true);

        }

        return result;
    }

    @Override
    public Result modifyUserInfo() {
        return null;
    }
}
